/*
 * page.h
 *
 *  Created on: 2012-6-27
 *      Author: monkey
 */
#ifndef __PAGE_H_
#define __PAGE_H_

#include <list.h>
#include <threads/synch.h>
#include <threads/palloc.h>
#include <threads/vaddr.h>
#include <vm/frame.h>
#include <bitmap.h>

enum sup_page_status
{
    SP_SUCCESS,
    SP_OUT_OF_MEM,
    SP_MAPPED,
    SP_NOT_FOUND,
    SP_UNMAPPED
};

struct SPTE
{
    struct list_elem elem;
    //struct lock lock;
    uintptr_t page_number;
    uintptr_t frame_number;
    //bool writable;
};

struct sup_page_table
{
    void *allocated_page;
    unsigned int free_offset;
    struct lock lock;
    //void *next_stack;
    struct list table;
};

struct exe_map
{
    void *upage;
    uint32_t file_offset;
    uint16_t read_bytes;
    bool writable;
    struct list_elem elem;
};

/*往补充页表中添加一个从page_n到frame_n的映射，
 *如果已经存在page_n的映射，则返回SP_MAPPED，
 *如果无法分配空间，则返回SP_OUT_OF_MEM,
 *如果成功则返回SP_SUCCESS*/
enum sup_page_status
page_map(struct sup_page_table *SPT,uintptr_t page_n,uintptr_t frame_n, bool writable);

/*取消page_n的映射，但不删除这个列表元素，而是把对应的frame_n设为NULL
 *如果不存在page_n的映射*/
bool
page_unmap(struct thread *t,uintptr_t page_n);

/*根据page_n找到一个对应的节点，找不到则返回NULL*/
struct SPTE *
SPTE_find(struct sup_page_table *SPT,uintptr_t page_n);

/*初始化函数*/
void SPT_init(struct sup_page_table *SPT);

/*page_n是否已映射*/
bool page_is_mapped(struct sup_page_table *SPT,uintptr_t page_n);



#endif

